<template>
  <div class="goods">
    <div class="wrap">
      <Crumb :data="nav"> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span :class="{ active: active1 == index }" v-for="item, index in options1" :key="index"
            @click="options1Fn(index, item.min, item.max)">{{ item.txt }}</span>

        </li>
        <li>
          <strong>分类：</strong>
          <span :class="{ active: item.inShow }" v-for="item, index in options2 " :key="index"
            @click="options2Fn(index, item.type)">{{
                item.txt
            }}</span>

        </li>
      </ul>
      <ul id="box">
        <List v-for="item in arrListShow" :key="item.id" :data="item"></List>
        <div>
          {{ isReachBottom ? "加载完成" : "正在加载" }}
        </div>
      </ul>
    </div>
  </div>
</template>                            
<script>

import Crumb from "../components/Crumb.vue";
import List from "../components/List.vue";
import { ProductsAPI } from "@/request/api"
import idnex from '@/store/loginStatus/idnex';
import { getScrollTop, getClientHeight, getScrollHeight } from "@/Utils/index"
export default {
  data() {
    return {
      nav: [
        { name: "首页", link: "#" },
        { name: "全部商品", link: "#" },
      ],
      // 栏目
      did: 0,
      // 类型
      type: 0,
      // 最小积分,
      min: 0,
      // 最大积分
      max: 0,
      // 关键字
      keyword: "",
      // 商品数组
      arrList: [],
      // 加载显示
      arrListShow: [],
      // 排序的数组项
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 
      options2: [
        { txt: "全部商品", type: 0, inShow: true },
        { txt: "事物礼品", type: 1, inShow: false },
        { txt: "虚拟礼品", type: 2, inShow: false }
      ],
      // 高亮
      active1: 0,
      // 当前页数
      page: 1,
      // 每页8条
      size: 8,
      isLoading: false,
      isReachBottom: false
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    // 搜索关键字keyword
    this.keyword = this.$route.query.keyword
    this.Products()
  },
  methods: {
    async Products() {
      let res = await ProductsAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      })
      this.arrList = res.data.data
      this.arrListShow = this.arrList.filter((item, index) => index < 8)
      this.page = 1
      this.isReachBottom = false
      if (this.arrList.length == this.arrListShow.length) {
        this.isReachBottom = true;
        return
      }
      // console.log(this.arrList);

    },
    options1Fn(index, min, max) {
      this.active1 = index
      this.min = min
      this.max = max
      this.keyword = ""
      this.Products()
    },
    options2Fn(index, type) {
      this.options2.forEach(item => {
        item.inShow = false
      })
      this.options2[index].inShow = true
      this.type = type
      this.keyword = ""
      this.Products()
    },
    scrollFn() {

      if (this.arrList.length == this.arrListShow.length) {
        this.isReachBottom = true;
        return
      }
      // console.log(getScrollTop(), getClientHeight(), getScrollHeight());
      if (getScrollTop() + getClientHeight() > getScrollHeight() - 300) {
        if (this.isLoading == false) {
          this.isLoading = true
          this.page++
          //  page:1,size8 index 0-7数据 
          // page:2 size:8 index 8-15 数据
          // page:3 size:8 index 16-23 数据
          setTimeout(() => {
            for (var i = (this.page - 1) * this.size; i < this.page * this.size; i++) {
              this.arrList[i] && this.arrListShow.push(this.arrList[i])
              console.log("for", this.page, this.arrList[i]);
            }
            this.isLoading = false
          }, 1500)

        }
      }

    }
  },
  mounted() {
    //  监听滚动事件
    window.addEventListener("scroll", this.scrollFn)
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn)
  },
  watch: {
    "$route.query.keyword": {
      handler(n, o) {
        // console.log(n, o);
        this.keyword = n
        this.Products()
      }
    }
  }

};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

#box {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -10px;

  li {
    margin: 0 10px;
  }
}

* {
  list-style: none;
}

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;

  .options {
    padding-top: 20px;
    padding-bottom: 40px;

    li {
      margin-top: 20px;
      text-align: none;

      strong {
        color: #000;
        font-weight: bold;
      }

      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;

        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>